package model
{
	import model.moving.Movement;
	import model.attack.Attack;
	import model.attack.NormalAttack;
	import model.moving.NormalMove;
	
	/**
	 * Class represents an individual square
	 */
	public class Piece
	{
		private var name:String;
		private var power:Number;
		private var source:String;
		private var x:Number;
		private var y:Number;
		private var moveMode:Movement;
		private var attackMode:Attack;
		
		public function Piece(name:String = 'Unknown', 
			power:Number = 2, source:String = 'images/bdroid.jpg', 
			move:Movement = null, attack:Attack = null){
			
			this.name = name;
			this.power = power;
			this.source = source;
			this.moveMode = move;
			this.attackMode = attack;
			
			if (this.moveMode == null) this.moveMode = new NormalMove();
			if (this.attackMode == null) this.attackMode = new NormalAttack();
		}
		
		/** Returns name string of the piece */
		public function getName():String{
			return this.name;
		}
		/** Returns the power number of the piece */
		public function getPower():Number{
			return this.power;
		}
		/** returns the string of the image source */
		public function getSource():String{
			return this.source;
		}
		/** causes an attack to take place */
		public function attack(defender:Piece):Piece{
			return this.attackMode.getWinner(this,defender);
		}
		/** the overriden toString method */
		public function toString():String{
			return this.name;
		}
		/** sets the X and Y grid coordinates for the piece */
		public function setXY(x:Number, y:Number):void{
			this.x = x;
			this.y = y;
		}
		/** Returns the X grid coordinate of the piece */
		public function getX():Number{
			return this.x;
		}
		/** Return the Y grid value of the piece */
		public function getY():Number{
			return this.y;
		}
		/** return whether the requested move is valid or not */
		public function move(newX:Number, newY:Number):Boolean{
			return this.moveMode.move(this.x, this.y, newX,newY);
		}
		/** Is the piece moveable or fixed on a certain position on the board */
		public function isMoveable():Boolean{
			if (this.name == 'bomb' || this.name == 'flag'){
				return false;
			}
			else return true;
		}
	}
}